<template>
  <el-table
    :data="tableData"
    style="width: 100%"
    height="calc(100% - 90px)"
    size="large"
    highlight-current-row
  >
    <el-table-column label="订单ID" sortable prop="id" width="200px" align="center" />
    <el-table-column
      label="订单名称"
      prop="name"
      width="200px"
      align="center"
      show-overflow-tooltip
    />
    <el-table-column label="付款金额" prop="price" align="center" sortable>
      <template #default="scope"> {{ scope.row.price }}.00 ￥</template>
    </el-table-column>
    <el-table-column label="用户名" prop="userName" width="200px" align="center" />
    <el-table-column label="联系电话" prop="number" width="200px" align="center" />
    <el-table-column label="订单状态" prop="status" width="200px" align="center" sortable>
      <template #default="scope">
        <el-tag v-if="scope.row.status == 1" style="margin: 0 5px" type="warning">待确认</el-tag>
        <el-tag v-if="scope.row.status == 2" style="margin: 0 5px">已确定</el-tag>
        <el-tag v-if="scope.row.status == 3" style="margin: 0 5px" type="success">已完成</el-tag>
        <el-tag v-if="scope.row.status == 4" style="margin: 0 5px" type="danger">已取消</el-tag>
        <el-tag v-if="scope.row.status == 5" style="margin: 0 5px" type="warning">待支付</el-tag>
        <el-tag v-if="scope.row.status == 6" style="margin: 0 5px" type="success">已完成</el-tag>
      </template>
    </el-table-column>
    <el-table-column label="创建时间" align="center" sortable>
      <template #default="scope"> {{ scope.row.createTime.substring(0, 10) }}</template>
    </el-table-column>
    <el-table-column align="center" width="250px">
      <template #header>
        <el-input v-model="search" size="default" placeholder="Type to search" />
      </template>
      <template #default="scope">
        <el-button
          size="small"
          v-if="scope.row.status == 1"
          @click="handleOk(scope.row)"
          type="primary"
          >确认订单</el-button
        >
        <el-button
          size="small"
          v-if="scope.row.status == 1"
          @click="handleCancel(scope.row)"
          type="danger"
          >取消订单</el-button
        >
        <el-button size="small" v-if="scope.row.status > 1" type="success">订单已处理</el-button>
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页 -->
  <div class="next">
    <el-pagination
      background
      layout="prev, pager, next"
      :total="count"
      :page-size="7"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script lang="ts" setup>
import axios from '@/utils/request'
import 'animate.css'
import { onMounted, ref } from 'vue'
import { io } from 'socket.io-client'
let count = ref()
let tableData = ref([])
const search = ''
const init = async () => {
  let countData = await axios.get('/order/count')
  count.value = countData.data
  const result = await axios({
    method: 'get',
    url: 'https://www.tinyxu.fun/v1/order/page',
    params: {
      page: 1,
      take: 15,
    },
  })
  tableData.value = result.data
}
onMounted(() => {
  init()
  const socket = io('https://www.tinyxu.fun/order')
  socket.on('addOrder', (data: boolean) => {
    init()
  })
})

//分页
const handleCurrentChange = async (page: any) => {
  const result = await axios({
    method: 'get',
    url: 'https://www.tinyxu.fun/v1/order/page',
    params: {
      page,
      take: 7,
    },
  })
  tableData.value = result.data
}

// 编辑
const handleOk = async (row: any) => {
  try {
    await axios.patch(`/order/${row.id}`, {
      status: 2,
    })
    ElMessage.success('确认成功')
    init()
  } catch (error) {
    ElMessage.error('出错了')
  }
}
//取消
const handleCancel = async (row: any) => {
  try {
    await axios.patch(`/order/${row.id}`, {
      status: 4,
    })
    ElMessage.success('取消成功')
    init()
  } catch (error) {
    ElMessage.error('出错了')
  }
}
// 删除操作
const handleDelete = async (row: any) => {
  try {
    await axios.delete(`/room/${row.id}`)
    ElMessage.success('删除成功')
    init()
  } catch (error) {
    ElMessage.error('出错了!删除失败')
  }
}
</script>
<style lang="less" scoped>
img {
  width: 70px;
  height: 70px;
  object-fit: contain;
  border-radius: 8px;
}

.next {
  height: 80px;
  display: flex;
  justify-content: flex-end;
}
</style>
